package com.lc.zpyh.ui.activity.order;

import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.load.MultiTransformation;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.hjq.http.EasyHttp;
import com.hjq.http.listener.HttpCallback;
import com.hjq.toast.ToastUtils;
import com.lc.zpyh.R;
import com.lc.zpyh.app.AppActivity;
import com.lc.zpyh.http.glide.GlideApp;
import com.lc.zpyh.http.request.ConfirmReceiptApi;
import com.lc.zpyh.http.request.OrderDetailApi;
import com.lc.zpyh.http.request.PublicMsgBean;
import com.lc.zpyh.http.response.DetailBean;
import com.lc.zpyh.other.IntentKey;
import com.lc.zpyh.util.BigDecimalUtils;
import com.lc.zpyh.util.constant.EvenConstant;
import com.lc.zpyh.util.eventbus.Event;

import org.jetbrains.annotations.NotNull;

import androidx.core.widget.NestedScrollView;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class OrderServedDetailsActivty extends AppActivity {

    @BindView(R.id.tv_people)
    TextView tvPeople;
    @BindView(R.id.tv_phone)
    TextView tvPhone;
    @BindView(R.id.tv_address)
    TextView tvAddress;
    @BindView(R.id.tv_total_price)
    TextView tvTotalPrice;
    @BindView(R.id.tv_yunfei)
    TextView tvYunfei;
    @BindView(R.id.tv_final_price)
    TextView tvFinalPrice;
    @BindView(R.id.tv_order_tag)
    TextView tvOrderTag;
    @BindView(R.id.tv_copy)
    TextView tvCopy;
    @BindView(R.id.tv_1)
    TextView tv1;
    @BindView(R.id.tv_order_time)
    TextView tvOrderTime;
    @BindView(R.id.btn_status)
    TextView btnStatus;
    @BindView(R.id.nestedscrollview)
    NestedScrollView nestedscrollview;
    @BindView(R.id.tv_shangjiamingcheng)
    TextView tv_shangjiamingcheng;
    @BindView(R.id.tv_beizhu)
    TextView tv_beizhu;
    @BindView(R.id.rv_shitang)
    RecyclerView rv_shitang;
    @BindView(R.id.tv_coupon)
    TextView tvCoupon;
    @BindView(R.id.tv_qucan)
    TextView tvQucan;
    @BindView(R.id.tv_qucan_code)
    TextView tvQucanCode;
    @BindView(R.id.pay_layout)
    RelativeLayout payLayout;
    @BindView(R.id.flow_layout)
    RelativeLayout flowLayout;
    @BindView(R.id.tv_pay_time)
    TextView tvPayTime;
    @BindView(R.id.tv_flow_no)
    TextView tvFlowNo;
    @BindView(R.id.add_layout)
    RelativeLayout addLayout;
    @BindView(R.id.tv_add_time)
    TextView tvAddTime;
    @BindView(R.id.tv_outTime)
    TextView tvOutTime;
    private String order_id = "";
    private BaseQuickAdapter<String, BaseViewHolder> shangjiaadapter;
    private String system_id;

    @Override
    protected int getLayoutId() {
        return R.layout.activty_orderserveddetails;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);
        Intent intent = getIntent();
        order_id = intent.getStringExtra("orderid");
        system_id = intent.getStringExtra("system_id");
        btnStatus.setText("确认收货");
        getOrderDetail();
    }

    private void getOrderDetail() {
        EasyHttp.post(this).api(new OrderDetailApi().setOrderid(order_id))
                .request(new HttpCallback<DetailBean>(this) {
                    @Override
                    public void onSucceed(DetailBean data) {
                        //订单状态(0未支付,1支付成功,2配餐中,4配送中,5已存柜,6已接收,7已失效,8已删除(用户不可见))9 无柜存餐待确认 10 已取消 11 失效已取出12支付失败
                        int state = data.getList().getOrderState();
                        if (state != 0) {
                            payLayout.setVisibility(View.VISIBLE);
                            flowLayout.setVisibility(View.VISIBLE);
                            tvPayTime.setText(data.getList().getOrderPaymentTime());
                            tvFlowNo.setText(data.getList().getPaymentOrder());
                        } else {
                            payLayout.setVisibility(View.GONE);
                            flowLayout.setVisibility(View.GONE);
                        }
                        if (state == 4 || state == 5 || state == 6 || state == 9 || state == 11) {
                            addLayout.setVisibility(View.VISIBLE);
                            tvAddTime.setText(data.getList().getCateringFinishTime());
                        } else {
                            addLayout.setVisibility(View.GONE);
                        }
                        tvQucanCode.setText(data.getList().getTakeCode());
                        tv_shangjiamingcheng.setText(data.getList().getSjmc());
                        tvPeople.setText(data.getList().getShr());
                        tvAddress.setText(data.getList().getGjmc());
                        tv_beizhu.setText(data.getList().getOrderNote());
                        tvOrderTime.setText(data.getList().getPlaceOrderTime());
                        tvOrderTag.setText(data.getList().getOrderNumber());
                        tvOutTime.setText("订单超过"+data.getList().getOutTime()+"分钟后就是失效了哦~ 请尽快取出");
                        tvFinalPrice.setText("￥" + BigDecimalUtils.add(BigDecimalUtils.div(data.getList().getActualAmountPaid() + "", "100", 2), BigDecimalUtils.div(data.getList().getOrderFreight() + "", "100", 2), 2));
                        tvTotalPrice.setText("￥" + BigDecimalUtils.div(data.getList().getOrderPrice() + "", "100", 2));
                        tvCoupon.setText("-￥" + BigDecimalUtils.div(data.getList().getCouponDiscountAmount() + "", "100", 2));
                        tvPhone.setText(data.getList().getShdh());
                        tvYunfei.setText("￥" + BigDecimalUtils.div(data.getList().getOrderFreight() + "", "100", 2));
                        BaseQuickAdapter<DetailBean.OrderDetailsListDTO, BaseViewHolder> sjadapter = new BaseQuickAdapter<DetailBean.OrderDetailsListDTO, BaseViewHolder>(R.layout.item_shop_confirm_shangjia_order, data.getList().getOrderDetailsList()) {
                            @Override
                            protected void convert(@NotNull BaseViewHolder baseViewHolder, DetailBean.OrderDetailsListDTO bean) {

                                GlideApp.with(getActivity())
                                        .load(IntentKey.ORDER_IMAGE + bean.getMainImg())
                                        .transform(new MultiTransformation<>(new CenterCrop(), new CircleCrop()))
                                        .into((ImageView) baseViewHolder.getView(R.id.img_dianpu));
                                baseViewHolder.setText(R.id.goods, bean.getCommodityName());
                                if (bean.getSpecificationName()==null){
                                    String condimentsName;
                                    if (bean.getCondimentsName()==null){
                                        condimentsName="";

                                    }else {
                                        condimentsName= bean.getCondimentsName();
                                    }
                                    baseViewHolder.setText(R.id.tv_yueshou,"默认"+"  "+condimentsName);
                                }else {
                                    String condimentsName;
                                    if (bean.getCondimentsName()==null){
                                        condimentsName="";

                                    }else {
                                        condimentsName= bean.getCondimentsName();
                                    }
                                    baseViewHolder.setText(R.id.tv_yueshou,bean.getSpecificationName()+"  "+condimentsName);
                                }
                                baseViewHolder.setText(R.id.tv_xianjia, "￥" + BigDecimalUtils.div(bean.getCommodityPrice() + "", "100", 2));
                                baseViewHolder.setText(R.id.tv_num, "X" + bean.getCount());
                            }
                        };
                        rv_shitang.setAdapter(sjadapter);
                    }

                    @Override
                    public void onFail(Exception e) {
                        super.onFail(e);
                    }
                });
    }

    @OnClick({R.id.btn_status, R.id.tv_copy, R.id.image_back})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_status:
                confirmReceipt();
                break;
            case R.id.image_back:
                finish();
                break;
            case R.id.tv_copy:
                ClipboardManager cm = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
                // 将文本内容放到系统剪贴板里。
                cm.setText(tvOrderTag.getText());
                ToastUtils.show("复制成功");
                break;
        }
    }

    private void confirmReceipt() {
        EasyHttp.post(this)
                .api(new ConfirmReceiptApi().setOrderId(order_id)).request(new HttpCallback<PublicMsgBean>(this) {

            @Override
            public void onSucceed(PublicMsgBean data) {
                if (data.getCode() == 0) {
                    toast(data.getList());
                    finish();

                }
            }

            @Override
            public void onFail(Exception e) {
                super.onFail(e);

            }
        });
    }

    @Override
    protected boolean isRegisterEventBus() {
        return true;
    }

    @Override
    protected void receiveEvent(Event event) {
        super.receiveEvent(event);
        if (event.getCode() == EvenConstant.PAYSTAUTS) {
//
            finish();
        }
    }
}
